package com.xunyin.modules.storeroom.rest;

import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.storeroom.domain.TStoreRoom;
import com.xunyin.modules.storeroom.service.TStoreRoomService;
import com.xunyin.modules.storeroom.service.dto.TStoreRoomQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author admin
 * @date 2022-09-19
 */
@Api(tags = "共享空间:房间管理")
@RestController
@RequestMapping("api")
public class StoreRoomController {

    private final IGenerator generator;

    private final TStoreRoomService tStoreRoomService;

    public StoreRoomController(TStoreRoomService tStoreRoomService, IGenerator generator) {
        this.tStoreRoomService = tStoreRoomService;
        this.generator = generator;
    }

    @Log("查询房间")
    @ApiOperation(value = "查询房间")
    @GetMapping(value = "/tStoreRoom")
    //@PreAuthorize("hasAnyRole('admin','MAINPART_ALL','MAINPART_SELECT')")
    public ResponseEntity getTStoreRoom(TStoreRoomQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> queryAll = tStoreRoomService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增房间")
    @ApiOperation(value = "新增房间")
    @PostMapping(value = "/tStoreRoom")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TStoreRoom resources) {
        resources.setStatus(1);
        BigDecimal bigDecimal = new BigDecimal(100);
        BigDecimal price = bigDecimal.multiply(resources.getPrice());
        resources.setPrice(price);
        return new ResponseEntity<>(tStoreRoomService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改房间")
    @ApiOperation(value = "修改房间")
    @PutMapping(value = "/tStoreRoom")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TStoreRoom resources) {
        BigDecimal bigDecimal = new BigDecimal(100);
        BigDecimal price = bigDecimal.multiply(resources.getPrice());
        resources.setPrice(price);
        tStoreRoomService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/tStoreRoom/{id}")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        tStoreRoomService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }

/*    @Log("根据门店id获取房间列表")
    @ApiOperation("根据门店id获取房间列表")
    @PostMapping(value = "/listRoom")
    // @PreAuthorize("@el.check('yxSystemStore:list')")
    public ResponseEntity<Object>  listRoom(@Validated @RequestBody TStoreRoomQueryParam queryParam) {

        return new ResponseEntity<>(tStoreRoomService.listRoom(queryParam),HttpStatus.OK);

    }*/
}
